<?php

namespace app\crm\controller;

use app\crm\model\Withdrawal as WithdrawalModel;


class Withdrawal extends BaseController
{
    public function getList($start = '', $end = '', $user_id = '', $status = '')
    {
        $where = [];
        $url_param = [];
        if (!empty($start)) {
            $where['add_time'][] = ['>=', $start];
            $url_param['query']['start'] = $start;
        }
        if (!empty($end)) {
            $where['add_time'][] = ['<=', $end];
            $url_param['query']['end'] = $end;
        }
        if (!empty($user_id)) {
            $where['user_id'] = $user_id;
            $url_param['query']['user_id'] = $user_id;
        }
        if (!empty($status)) {
            $where['status'] = $status;
//            $url_param['query']['start'] = $start;
        }
        $list = WithdrawalModel::where($where)->order('add_time desc')->paginate(10, false, $url_param);
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function test()
    {
        (new WithdrawalModel())->alipayTransfer('');
    }

    //审核通过
    public function pass($order_no, $pass)
    {
        $info = WithdrawalModel::get(['order_no' => $order_no]);
        if (!$info) {
            return json(['errorCode' => 1, 'msg' => '参数错误']);
        }
        if ($pass) {
            $info->status = config('status.提现状态_已审核');
            $flag = true;
        } else {
            $info->status = config('status.提现状态_审核不通过');
            $flag = false;
        }
        $info->audit_time = time();
        $info->auditor = session('manage_id');
        $res = $info->save();

        if ($res) {
            //TODO 自动提现
            $result = WithdrawalModel::alipayTransfer($info);
        } else {
            return json(['errorCode' => 1, 'msg' => '操作失败']);
        }
    }

    //审核成功操作
    private function passSuccess($flag)
    {
        if ($flag) {
            //todo 支付宝提现
            //TODO 扣除金额进入奖金池
        } else {
            //TODO 退回提现金额
        }
    }
}